package com.localization;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.myprojectoffeixue.MainActivity;

/**
 * Created by ASUS on 2017/3/8.
 */
public class SendUtil {
    public static boolean flag=false;

    public static String Mac;
    public static String cmd;
    public static Handler hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            flag=true;
            Log.d("收到消息","收到");
            super.handleMessage(msg);
        }
    };

    public static Thread SendThread=new Thread(){
        @Override
        public void run() {
            Log.d("开启","开启");
            while(true){
           if(flag){
               flag=false;
               if(GlobalValue.mGateway.MacSocke!=null&&GlobalValue.mGateway.MacSocke.containsKey(Mac)){
              GlobalValue.mGateway.MacSocke.get(Mac).send(chatOrders(cmd));}
             Log.d("命令发送成功","成功"+cmd);}
            }
        }
    };


    public static byte[] chatOrders(String c) {
        byte[] m = c.getBytes();
        return chatOrder(m);
    }


    public static byte[] chatOrder(byte[] m) {
        if (m.length % 2 == 0) {
            byte[] bytes = new byte[m.length / 2];
            for (int i = 0, j = 0; i < m.length; i += 2, j++) {
                bytes[j] = uniteByte(m[i], m[i + 1]);
            }
            return bytes;
        }
        return null;
    }
    public static byte uniteByte(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }






}
